package org.example.panel.coursepanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class AddCoursePanel extends JPanel {
    public AddCoursePanel() {
        //组件准备
        JTextField course = new JTextField(15);
        JButton addCourse = new JButton("添加课程");
        Box courseBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        addCourse.addActionListener((ActionEvent e) -> {
            if(course.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "课程名不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(course.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "课程名不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(CourseOperation.isCourse(course.getText())) {
                JOptionPane.showMessageDialog(null,
                        "课程已存在", null, JOptionPane.WARNING_MESSAGE);
            }else{
                CourseOperation.addCourse(course.getText());
                JOptionPane.showMessageDialog(null,
                        "添加课程成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        courseBox.add(new JLabel("课程名： "));
        courseBox.add(course);
        box.add(courseBox);
        box.add(Box.createVerticalStrut(5));
        box.add(addCourse);
        add(box);
    }
}
